RStudio Exercise 4: Classification and Clustering

The fourth Rstudio exercise is on classification and clustering. The data we use for this exercise is the Boston data from the MASS package.

2. Boston dataset

The Boston dataset deals with the housing values in the suburbs of Boston. The attributes (variables) of the Boston data are the following:

  1. crim - per capita crime rate by town.
  2. zn - proportion of residential land zoned for lots over 25,000 sq.ft.
  3. indus - proportion of non-retail business acres per town.
  4. chas - Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).
  5. nox - nitrogen oxides concentration (parts per 10 million).
  6. rm - average number of rooms per dwelling.
  7. age - proportion of owner-occupied units built prior to 1940.
  8. dis - weighted mean of distances to five Boston employment centres.
  9. rad - index of accessibility to radial highways.
  10. tax - full-value property-tax rate per $10,000.
  11. ptratio - pupil-teacher ratio by town.
  12. black - 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town.
  13. lstat - lower status of the population (percent).
  14. medv - median value of owner-occupied homes in $1000s.
# access the MASS package
library(MASS)

# load the data
data("Boston")

# explore the dataset
str(Boston)
## 'data.frame':    506 obs. of  14 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : int  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ black  : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
table(is.na(Boston))
## 
## FALSE 
##  7084

The Boston dataset has 506 observations of 14 variables. The datset has no missing values or NAs. The summary gives the minimum, median, maximum and the quartile values of the variables of the Boston dataset.

3. Graphical overview and the Summaries of the Boston dataset

The summary of the Boston data gives the quartiles, minimum and maximum values of the variables in the Boston dataset.

summary(Boston)
##       crim                zn             indus            chas        
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.46   Min.   :0.00000  
##  1st Qu.: 0.08204   1st Qu.:  0.00   1st Qu.: 5.19   1st Qu.:0.00000  
##  Median : 0.25651   Median :  0.00   Median : 9.69   Median :0.00000  
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.14   Mean   :0.06917  
##  3rd Qu.: 3.67708   3rd Qu.: 12.50   3rd Qu.:18.10   3rd Qu.:0.00000  
##  Max.   :88.97620   Max.   :100.00   Max.   :27.74   Max.   :1.00000  
##       nox               rm             age              dis        
##  Min.   :0.3850   Min.   :3.561   Min.   :  2.90   Min.   : 1.130  
##  1st Qu.:0.4490   1st Qu.:5.886   1st Qu.: 45.02   1st Qu.: 2.100  
##  Median :0.5380   Median :6.208   Median : 77.50   Median : 3.207  
##  Mean   :0.5547   Mean   :6.285   Mean   : 68.57   Mean   : 3.795  
##  3rd Qu.:0.6240   3rd Qu.:6.623   3rd Qu.: 94.08   3rd Qu.: 5.188  
##  Max.   :0.8710   Max.   :8.780   Max.   :100.00   Max.   :12.127  
##       rad              tax           ptratio          black       
##  Min.   : 1.000   Min.   :187.0   Min.   :12.60   Min.   :  0.32  
##  1st Qu.: 4.000   1st Qu.:279.0   1st Qu.:17.40   1st Qu.:375.38  
##  Median : 5.000   Median :330.0   Median :19.05   Median :391.44  
##  Mean   : 9.549   Mean   :408.2   Mean   :18.46   Mean   :356.67  
##  3rd Qu.:24.000   3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.23  
##  Max.   :24.000   Max.   :711.0   Max.   :22.00   Max.   :396.90  
##      lstat            medv      
##  Min.   : 1.73   Min.   : 5.00  
##  1st Qu.: 6.95   1st Qu.:17.02  
##  Median :11.36   Median :21.20  
##  Mean   :12.65   Mean   :22.53  
##  3rd Qu.:16.95   3rd Qu.:25.00  
##  Max.   :37.97   Max.   :50.00

We use the correlation between the variables of the data. We create a corr_matrix by using the cor() function on Boston data. To visualize the correlation we use the corr_plot funtion from the corr_plot package. As the correlation matrix is symmetric we need only upper or lower matrix. Here we get the correlation as numbers.

library(corrplot); library(dplyr)
cor_matrix<-cor(Boston) %>% round(digits=2)

# visualize the correlation matrix
corrplot(cor_matrix, method="number", type = "upper", cl.pos = "b", tl.pos = "d", tl.cex = 0.6)

We can observe that the variable “crim”(percapita crime rate by town) has a high correlation to “rad” (index of accessibility to radial highways). Also a high correlation (0.) between “indus” (non retail business acres may be industrial places) to “nox” nitrogen oxide. Also “nox”" and “zn” (residential lands zoned for lots) and so on. Another observation is that “tax” and “rad” are highly correlated (0.91). The “dis”, the distance to the city centers and age of the building are negatively correlated (-0.7). The old building are away from the city centers. Also we can see the nox and dis are negatively correlated.

Scatter plots and distributions

Here is another way of showing the distributions of the variables and the correlation between pairs using the ggpairs function.

library (ggplot2)
library (GGally)
ggpairs(Boston, mapping = aes(), lower = list(combo = wrap("facethist", bins = 20))) + ggtitle("Matrix of scatter plots and distributions")

5. Stardardizing the Boston data

Since the variables of the Boston data set has quite different range for their values for example: rm (number of rooms) and tx (full value tax rate) we scale the data. For standardizing or scaling we subtract the column means from the corresponding columns and divide the difference with standard deviation.

Scaling makes the variables normally distributed and each variable has the same variance. Later when we do the linear discriminant analysis (LDA), the requirement on the data is that all the variables in the data are normally distributed and they should have the same variance. We can see that in the scaled version of Boston data which is in boston_scaled, the values of all the variables have comparable ranges. The boston_scaled is a matrix and we change it to a dataframe from easily accessing the variables.

# center and standardize variables
boston_scaled <- scale(Boston)

# summaries of the scaled variables
summary(boston_scaled)
##       crim                 zn               indus        
##  Min.   :-0.419367   Min.   :-0.48724   Min.   :-1.5563  
##  1st Qu.:-0.410563   1st Qu.:-0.48724   1st Qu.:-0.8668  
##  Median :-0.390280   Median :-0.48724   Median :-0.2109  
##  Mean   : 0.000000   Mean   : 0.00000   Mean   : 0.0000  
##  3rd Qu.: 0.007389   3rd Qu.: 0.04872   3rd Qu.: 1.0150  
##  Max.   : 9.924110   Max.   : 3.80047   Max.   : 2.4202  
##       chas              nox                rm               age         
##  Min.   :-0.2723   Min.   :-1.4644   Min.   :-3.8764   Min.   :-2.3331  
##  1st Qu.:-0.2723   1st Qu.:-0.9121   1st Qu.:-0.5681   1st Qu.:-0.8366  
##  Median :-0.2723   Median :-0.1441   Median :-0.1084   Median : 0.3171  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.:-0.2723   3rd Qu.: 0.5981   3rd Qu.: 0.4823   3rd Qu.: 0.9059  
##  Max.   : 3.6648   Max.   : 2.7296   Max.   : 3.5515   Max.   : 1.1164  
##       dis               rad               tax             ptratio       
##  Min.   :-1.2658   Min.   :-0.9819   Min.   :-1.3127   Min.   :-2.7047  
##  1st Qu.:-0.8049   1st Qu.:-0.6373   1st Qu.:-0.7668   1st Qu.:-0.4876  
##  Median :-0.2790   Median :-0.5225   Median :-0.4642   Median : 0.2746  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.6617   3rd Qu.: 1.6596   3rd Qu.: 1.5294   3rd Qu.: 0.8058  
##  Max.   : 3.9566   Max.   : 1.6596   Max.   : 1.7964   Max.   : 1.6372  
##      black             lstat              medv        
##  Min.   :-3.9033   Min.   :-1.5296   Min.   :-1.9063  
##  1st Qu.: 0.2049   1st Qu.:-0.7986   1st Qu.:-0.5989  
##  Median : 0.3808   Median :-0.1811   Median :-0.1449  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.4332   3rd Qu.: 0.6024   3rd Qu.: 0.2683  
##  Max.   : 0.4406   Max.   : 3.5453   Max.   : 2.9865
# class of the boston_scaled object is a matrix
#class(boston_scaled)

# change the matrix object boston_scaled to a data frame
boston_scaled <- as.data.frame(boston_scaled)

Create a categorical variable for crime

The “crim”" variable will be better understood if we make it as a categorical variable based on its quantile values. We cut the variable crim by its quantiles to four categories, namely low, med_low, med_high and high.

# save the scaled crim as scaled_crim
scaled_crim <- boston_scaled$crim

# create a quantile vector of crim and print it
bins <- quantile(scaled_crim)
bins
##           0%          25%          50%          75%         100% 
## -0.419366929 -0.410563278 -0.390280295  0.007389247  9.924109610
# create a categorical variable 'crime'
crime <- cut(scaled_crim, breaks = bins, include.lowest = TRUE, labels = c("low", "med_low", "med_high", "high"))

# look at the table of the new factor crime

crime
##   [1] low      low      low      low      low      low      med_low 
##   [8] med_low  med_low  med_low  med_low  med_low  med_low  med_high
##  [15] med_high med_high med_high med_high med_high med_high med_high
##  [22] med_high med_high med_high med_high med_high med_high med_high
##  [29] med_high med_high med_high med_high med_high med_high med_high
##  [36] low      med_low  low      med_low  low      low      med_low 
##  [43] med_low  med_low  med_low  med_low  med_low  med_low  med_low 
##  [50] med_low  med_low  low      low      low      low      low     
##  [57] low      low      med_low  med_low  med_low  med_low  med_low 
##  [64] med_low  low      low      low      low      med_low  med_low 
##  [71] med_low  med_low  med_low  med_low  low      med_low  med_low 
##  [78] med_low  low      med_low  low      low      low      low     
##  [85] low      low      low      low      low      low      low     
##  [92] low      low      low      low      med_low  med_low  med_low 
##  [99] low      low      med_low  med_low  med_low  med_low  med_low 
## [106] med_low  med_low  med_low  med_low  med_high med_low  med_low 
## [113] med_low  med_low  med_low  med_low  med_low  med_low  med_low 
## [120] med_low  low      low      med_low  med_low  med_low  med_low 
## [127] med_high med_high med_high med_high med_high med_high med_high
## [134] med_high med_high med_high med_high med_high med_low  med_high
## [141] med_high med_high med_high high     med_high med_high med_high
## [148] med_high med_high med_high med_high med_high med_high med_high
## [155] med_high med_high med_high med_high med_high med_high med_high
## [162] med_high med_high med_high med_high med_high med_high med_high
## [169] med_high med_high med_high med_high med_low  med_low  med_low 
## [176] low      low      low      low      low      low      low     
## [183] med_low  med_low  med_low  low      low      low      med_low 
## [190] med_low  med_low  low      med_low  low      low      low     
## [197] low      low      low      low      low      low      low     
## [204] low      low      med_low  med_low  med_low  med_low  med_high
## [211] med_low  med_high med_low  med_low  med_high med_low  low     
## [218] low      med_low  med_low  med_high med_high med_high med_high
## [225] med_high med_high med_high med_high med_high med_high med_high
## [232] med_high med_high med_high med_high med_high med_high med_high
## [239] med_low  med_low  med_low  med_low  med_low  med_low  med_low 
## [246] med_low  med_high med_low  med_low  med_low  med_low  med_low 
## [253] med_low  med_high low      low      low      med_high med_high
## [260] med_high med_high med_high med_high med_high med_high med_high
## [267] med_high med_high med_high med_low  med_high med_low  med_low 
## [274] med_low  low      med_low  med_low  low      low      med_low 
## [281] low      low      low      low      low      low      low     
## [288] low      low      low      low      low      low      med_low 
## [295] low      med_low  low      med_low  low      low      low     
## [302] low      med_low  med_low  low      low      low      low     
## [309] med_high med_high med_high med_high med_high med_high med_high
## [316] med_low  med_high med_low  med_high med_high med_low  med_low 
## [323] med_high med_high med_high med_low  med_high med_low  low     
## [330] low      low      low      low      low      low      low     
## [337] low      low      low      low      low      low      low     
## [344] low      low      low      low      low      low      low     
## [351] low      low      low      low      low      med_low  high    
## [358] high     high     high     high     high     high     high    
## [365] med_high high     high     high     high     high     high    
## [372] high     high     high     high     high     high     high    
## [379] high     high     high     high     high     high     high    
## [386] high     high     high     high     high     high     high    
## [393] high     high     high     high     high     high     high    
## [400] high     high     high     high     high     high     high    
## [407] high     high     high     high     high     high     high    
## [414] high     high     high     high     high     high     high    
## [421] high     high     high     high     high     high     high    
## [428] high     high     high     high     high     high     high    
## [435] high     high     high     high     high     high     high    
## [442] high     high     high     high     high     high     high    
## [449] high     high     high     high     high     high     high    
## [456] high     high     high     high     high     high     high    
## [463] high     high     high     med_high high     high     high    
## [470] high     high     high     med_high high     high     high    
## [477] high     high     high     high     high     high     high    
## [484] med_high med_high med_high high     high     med_low  med_low 
## [491] med_low  med_low  med_low  med_low  med_high med_low  med_high
## [498] med_high med_low  med_low  med_low  low      low      low     
## [505] med_low  low     
## Levels: low med_low med_high high
# remove original crim from the dataset
boston_scaled <- dplyr::select(boston_scaled, -crim)

# add the new categorical value to scaled data
boston_scaled <- data.frame(boston_scaled, crime)

Create train and test data from boston_scaled

# number of rows in the Boston dataset 
n <- nrow(boston_scaled)

# choose randomly 80% of the rows
ind <- sample(n,  size = n * 0.8)

# create train set which has 80 % of the observations of the boston_scaled data 
train <- boston_scaled[ind,]

# create test set 
test <- boston_scaled[-ind,]

# save the crime observations of the test data in the variable correct classe. We use correct classes to evaluate the predictability of our LDA model.
correct_classes <- test$crime

# remove the crime variable from the test data
test <- dplyr::select(test, -crime)

Linear Discriminent analysis (LDA)

LDA is a classification method. So it is supervised learning. Logistic regression is a binary classification algorithm, whereas LDA can classify into multiple classes. In our problem the predictand “crime” has four categorical variables. With LDA, we train the model first with the predictors in the train set and then predict the test data into various crime categories. lda.fit is the LDA model we obtained with the train data.

lda.fit <- lda(crime ~., data = train)

# print the lda.fit object
lda.fit
## Call:
## lda(crime ~ ., data = train)
## 
## Prior probabilities of groups:
##       low   med_low  med_high      high 
## 0.2400990 0.2648515 0.2202970 0.2747525 
## 
## Group means:
##                  zn      indus        chas        nox         rm
## low       0.9018464 -0.8727836 -0.10997442 -0.8595645  0.4371354
## med_low  -0.1123658 -0.2479169 -0.01476176 -0.5421017 -0.1277584
## med_high -0.3995589  0.2669576  0.25851591  0.4810516  0.1114379
## high     -0.4872402  1.0149946 -0.05951284  1.0393726 -0.3690631
##                 age        dis        rad        tax     ptratio
## low      -0.9080829  0.8456909 -0.6870585 -0.7737905 -0.39708034
## med_low  -0.3812556  0.3062999 -0.5493177 -0.4599434 -0.02996814
## med_high  0.4717431 -0.4239147 -0.4089281 -0.2948110 -0.43980913
## high      0.8003735 -0.8385279  1.6596029  1.5294129  0.80577843
##                black       lstat         medv
## low       0.37731924 -0.76276906  0.551852088
## med_low   0.32445741 -0.14728270 -0.003770261
## med_high  0.03725688 -0.04158987  0.186282668
## high     -0.72102844  0.87871457 -0.686606158
## 
## Coefficients of linear discriminants:
##                 LD1         LD2         LD3
## zn       0.13748413  0.62928831 -0.95504329
## indus    0.11706694 -0.20201730  0.11945965
## chas    -0.06106388 -0.09341536  0.13953176
## nox      0.20287696 -0.77271401 -1.16570161
## rm      -0.16183430 -0.07832381 -0.14653525
## age      0.25846734 -0.44016027 -0.18609527
## dis     -0.08811682 -0.24521032  0.06384964
## rad      3.83131000  0.88507564 -0.48165465
## tax     -0.04653042 -0.08566456  0.85888503
## ptratio  0.12060328  0.18805628 -0.13464829
## black   -0.13992321  0.04496897  0.13368231
## lstat    0.16311854 -0.05069999  0.38553401
## medv     0.19634775 -0.26784013 -0.19303301
## 
## Proportion of trace:
##    LD1    LD2    LD3 
## 0.9612 0.0289 0.0099

The Linear Discriminant classification on the boston_scaled data into four categorical levels of the variable crime is summarized by the model lda.fit. Basically LDA is Bayes classifier. The prior probabilities of the groups(categorical levels of crime variable) areshown in the above table.

The group means of the 13 predictor variables are also shown. Group means is the average of each predictor within each class.

As there are four groups in the classifier we have three linear discriminants (LD1, LD2 and LD3) and the coefficient of LDs for each of the predictor variables are given. The proportion of trace indicates that 94.7 % of the variance is explained by LD1 followed by LD2 and LD3 explain 3.8 % and 1.4 % respectively.

The LDA biplot plots the coefficients of the predictor variables as a function of LDs. In the plot shown here we plot the coefficients of the predictor variables as a function of LD1 and LD2. The variable “rad”(index of accessibility to radial highways) has the highest value (3.2) with LD1 and we can see from the plot that “rad” has the largest length arrow in the LD1 direction so it is the most influential variable .

From the plot of LD1 vs LD2 we can see that we can clearly distinguish the “high_crime rate” from “low” and “medium_low”. A few “med_high” comes with “high”.

# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}
# target classes as numeric
classes <- as.numeric(train$crime)
# plot the lda results
plot(lda.fit, dimen = 2, col = classes, pch = classes)
lda.arrows(lda.fit, myscale = 1)

We also plotted LD1, LD2 and LD3 by changing the dimen variable in the above code to 3. Now we can see 6 biplots of LD1, LD2 and LD3. When we plot LD2 vs LD3, the crime levels cannot be distinguished as LD2 and LD3 explains only about 5 % of the variance. I do not understand why the arrows are not shown in the biplots except in LD3 vs LD3.

# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]],col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}
# target classes as numeric
classes <- as.numeric(train$crime)
# plot the lda results
plot(lda.fit, dimen = 3, col = classes, pch = classes)
lda.arrows(lda.fit, myscale = 0.25)

We also have the biplots based on the method given in the [stackoverflow answer] (http://stackoverflow.com/questions/17232251/how-can-i-plot-a-biplot-for-lda-in-r/17240647#17240647)

#install.packages("devtools")
#library(devtools)
#install_github("fawda123/ggord")
library(ggord)
ord <- lda(crime ~., data = train, prior = rep(1, 4)/4)
ggord(ord, train$crime)

LDA prediction

lda.fit is our LDA model and using that model we can see how the model classifies the test data in to different classes of the categorical variable “crime”.

Recall that when we portioned the data into train and test data, we saved the test$crime (actually the observations) in the variable “correct classes”.

From the table of correct classes and the predicted ones, we can see that the predictor correctly classifies into “high” crime class 28 out of 29 test observations. For the “medium_high”" test observations, there is almost half probability that they will be classified into “medium_low”. The “medium_low”s are classified correctly with a probability 13/27 and “low”s are correctly classified with a probability 13/22. In my view, a somewhat OK classifier as “high”s are clearly distinguished from the other classes.

# predict classes with test data
lda.pred <- predict(lda.fit, newdata = test)

# cross tabulate the results
table(correct = correct_classes, predicted = lda.pred$class)
##           predicted
## correct    low med_low med_high high
##   low       20      10        0    0
##   med_low    2      15        2    0
##   med_high   2      20       13    2
##   high       0       0        1   15

7. K-means algorithm

K-means algorithm is one of the simplest clustering algorithm. It portions n observations into k clusters. The clustering policy is that each observation belongs to the cluster with nearest mean. This is an unsupervised learning algorithm.

The k-means algorithm is an iterative algorithm and it works as follows. Initially we define k centroids for the k clusters. Take each observation (point) in the data set and associate it with a centroid for which the euclidean distance between the observation and the centroid is the least. When all the observations are associated with their own centroids, the first iteration step is over. Now re calculate the centroids and reassociate the points to the new centroids. These iteration steps continue till the centroids do not change anymore.

Now we perform the K-means clustering on the Boston dataset. We scale the dataset and calculate the distance matrix by finding the euclidean distance between the variables. #### Scaling the Boston data and finding the euclidean distance matrix

# access the MASS package
library(MASS)


# load the data
data("Boston")

# center and standardize variables
boston_scaled <- scale(Boston)

# euclidean distance matrix
dist_eu <- dist(boston_scaled)

# look at the summary of the distances
summary(dist_eu)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1343  3.4620  4.8240  4.9110  6.1860 14.4000

Finding the optimum number of clusters

We determine the number of clusters by plotting the the total of within cluster sum of squares (WCSS) behaves when the number of the clusters changes.

library(GGally)
library(ggplot2)

# set a seed value which enables to repeat the kmeans() function with the 
# same initial clusters. Otherwise the initial clusters may be assigned randomly.
set.seed(123)

# determine the number of clusters
k_max <- 15

# calculate the total within sum of squares
twcss <- sapply(1:k_max, function(k){kmeans(dist_eu, k)$tot.withinss})
twcss
##  [1] 748534.80 409297.55 340195.63 265150.70 195605.03 168935.61 156735.02
##  [8] 142248.04 117225.02 106470.11 102312.92  95290.78  94951.12  92602.61
## [15]  79854.73
# visualize the results
plot(1:k_max, twcss, type='b')

The plot of the total within sum of squares against number of clusters varying from 1 to 15 is shown in the figure above. The twcss varies from 748534.8 to 79854.73 for k = 1 to 15. We take k = 6 as optimal as it reduces the twcss distance by around 75 %.

Clustering using K-means algorithm

K-means clustering algorithm invoked here will place each of the observations in 6 clusters as we have given the centers as 6.

# Kmeans algorithm
km <-kmeans(dist_eu, centers = 6)


# the clustering information
km
## K-means clustering with 6 clusters of sizes 137, 48, 65, 104, 51, 101
## 
## Cluster means:
##          1        2        3        4        5        6        7        8
## 1 2.552963 2.191615 2.547985 2.481257 2.636215 2.126035 2.463163 3.260455
## 2 4.115845 4.367908 3.914214 4.415556 4.277226 4.521520 4.462950 4.591271
## 3 7.347551 6.917018 7.668887 8.029178 8.008552 7.594726 6.746593 6.719663
## 4 3.322873 2.419949 3.555546 3.901515 3.917887 3.259432 2.872770 3.145128
## 5 3.989644 4.556297 4.216580 3.977768 4.072917 4.089124 4.248097 4.850842
## 6 5.635860 5.015922 5.810840 6.243394 6.219170 5.797678 5.007134 5.045384
##          9       10       11       12       13       14       15       16
## 1 4.420281 3.050288 3.460627 2.800817 2.548810 2.498158 2.847417 2.519525
## 2 5.861940 4.897185 5.053174 4.724166 4.806196 5.041859 5.037049 5.123212
## 3 6.637733 6.672022 6.656885 6.725987 6.831291 6.522547 6.271019 6.514117
## 4 3.802664 3.078288 3.240106 2.958259 3.171899 2.294340 2.083575 2.330854
## 5 6.023239 4.730761 5.085827 4.546510 4.256435 5.022876 5.346690 5.064489
## 6 5.275376 5.034209 5.024306 5.008656 5.191566 4.483294 4.239085 4.500312
##         17       18       19       20       21       22       23       24
## 1 2.477710 2.972866 3.035484 2.815474 3.883785 3.047242 3.413583 3.706860
## 2 5.188031 5.159236 5.556072 5.185846 5.777782 5.080391 5.333884 5.590056
## 3 6.848068 6.136584 6.246617 6.243108 5.944153 6.154788 6.070741 6.047729
## 4 2.926372 2.006490 2.829410 2.073005 2.478201 1.996680 2.200197 2.363123
## 5 4.839017 5.494447 5.410418 5.409972 6.301722 5.559564 5.849309 6.119225
## 6 4.932293 4.118743 4.699219 4.231080 4.140866 4.107180 4.064205 4.123917
##         25       26       27       28       29       30       31       32
## 1 3.317906 3.492059 3.174430 3.407128 3.072595 2.917289 3.883866 3.341829
## 2 5.355804 5.648491 5.331440 5.423799 4.956368 4.757484 5.818922 5.290357
## 3 6.124494 5.850285 6.119300 5.848617 6.281408 6.323214 5.926407 6.111869
## 4 2.152358 2.399737 2.130704 2.313981 2.194705 2.226566 2.563711 2.185395
## 5 5.776453 5.909337 5.645826 5.792679 5.463721 5.291108 6.251560 5.790675
## 6 4.125236 4.229138 4.173627 4.154204 4.209655 4.253839 4.190000 4.132815
##         33       34       35       36       37       38       39       40
## 1 4.437132 3.655982 4.014459 2.356225 2.324457 2.085109 2.100542 3.469146
## 2 6.168152 5.623677 5.717537 4.728770 4.767845 4.847078 4.787796 5.456166
## 3 5.702765 5.899302 5.635547 6.472198 6.389736 6.751702 6.860251 8.586641
## 4 3.295133 2.297075 2.760000 2.036929 2.052171 2.484306 2.720323 5.019539
## 5 6.577978 6.113220 6.286376 5.003424 4.975828 4.672432 4.567581 2.437628
## 6 4.543962 4.083659 4.244460 4.504907 4.504214 4.851253 4.995628 6.988766
##         41       42       43       44       45       46       47       48
## 1 3.806323 2.464106 2.277093 2.245886 1.890630 2.124631 2.187826 2.870058
## 2 5.469978 4.947438 5.088076 5.079073 4.810066 5.080046 5.057556 5.071546
## 3 8.944151 7.985202 7.757987 7.727039 7.185569 7.146905 7.056687 6.715830
## 4 5.447668 4.089165 3.798403 3.739937 2.899376 2.938610 2.889613 2.590240
## 5 2.550075 4.003727 4.101822 4.105741 4.205545 4.493890 4.535762 5.127133
## 6 7.347068 6.266063 6.066725 6.010008 5.405380 5.401641 5.323536 4.969192
##         49       50       51       52       53       54       55       56
## 1 4.374890 2.484987 2.216591 2.155992 2.202966 2.192392 4.011314 4.620562
## 2 6.206363 5.152123 5.037840 4.798345 4.875844 5.098446 6.488456 6.248718
## 3 6.857506 6.903606 7.327416 7.333311 7.954340 7.750576 7.846534 9.656599
## 4 3.649819 2.729138 3.311553 3.194530 3.999725 3.806354 4.738612 6.360064
## 5 6.354068 4.788056 3.687296 3.635669 3.127248 3.365371 3.768251 2.573205
## 6 5.425549 5.185207 5.660587 5.607665 6.251113 6.093472 6.228695 8.172561
##         57       58       59       60       61       62       63       64
## 1 4.138053 4.788808 2.369128 2.238707 2.577586 3.001430 2.348247 2.423646
## 2 6.314081 6.255593 5.396948 5.291164 5.423722 5.411219 4.967901 5.080093
## 3 9.093653 9.562610 7.619436 7.182935 6.981514 6.757826 7.282673 7.580278
## 4 5.728217 6.380166 3.850185 3.257986 3.128914 3.007457 3.306577 3.782189
## 5 2.435993 2.586150 3.455264 3.762957 4.163099 4.634886 3.692547 3.384146
## 6 7.625711 8.169946 5.852530 5.357849 5.193164 4.988228 5.388941 5.749506
##         65       66       67       68       69       70       71       72
## 1 3.099810 3.755717 3.762213 2.164752 2.377335 2.067120 2.395258 2.279879
## 2 5.070095 5.920134 6.075955 5.331567 5.513971 5.232351 5.172823 5.258275
## 3 8.474576 8.706790 8.325184 7.481800 7.133931 7.317062 7.509722 7.139492
## 4 4.571372 5.363377 5.039822 3.540862 3.214330 3.286807 3.670384 3.233668
## 5 3.572872 2.365488 2.854471 3.828722 4.245479 3.861492 4.311560 4.482338
## 6 6.803815 7.202476 6.871459 5.702754 5.382884 5.506994 5.679697 5.332385
##         73       74       75       76       77       78       79       80
## 1 2.419650 2.395619 2.582255 2.208724 2.569263 2.284706 2.281898 2.322846
## 2 5.331200 5.256931 5.138078 4.739219 4.676271 4.796607 4.781211 5.037161
## 3 7.496987 7.426016 7.172186 6.550720 6.164051 6.445026 6.421550 6.621032
## 4 3.633211 3.612120 3.532216 2.554765 2.115305 2.435091 2.406743 2.698489
## 5 4.423047 4.374894 4.565665 4.571969 4.993178 4.715682 4.645174 4.703818
## 6 5.670343 5.628241 5.253844 4.504533 4.080734 4.392771 4.369389 4.613917
##         81       82       83       84       85       86       87       88
## 1 2.077143 2.128207 1.941076 1.906614 1.839655 1.960338 2.004413 2.011357
## 2 4.611003 4.507704 4.824876 4.799211 4.493640 4.250144 4.736784 4.543074
## 3 7.679009 7.247432 7.495607 7.269305 7.155841 7.257393 6.986917 6.981295
## 4 3.593223 2.978716 3.362220 3.049922 2.723815 2.845202 2.603436 2.477017
## 5 3.228722 3.647245 3.352955 3.510806 4.214393 4.216222 4.470996 4.539526
## 6 5.833680 5.345002 5.671433 5.439196 5.298921 5.378578 5.181960 5.119600
##         89       90       91       92       93       94       95       96
## 1 2.659893 2.401307 2.216247 2.322228 2.415933 2.468939 2.704529 2.182013
## 2 4.086432 3.906040 4.252949 4.267811 4.510455 4.766874 4.611235 4.143402
## 3 7.155386 7.332941 6.878036 6.862533 6.774331 7.083584 6.505216 7.188899
## 4 2.894806 3.107514 2.443156 2.419531 2.751041 3.222217 2.448424 2.937590
## 5 4.787782 4.431266 4.656816 4.754404 4.105175 4.001644 4.521147 4.347592
## 6 5.260876 5.460377 5.023131 4.997348 4.772459 5.169144 4.479851 5.437460
##         97       98       99      100      101      102      103      104
## 1 2.261612 3.692387 3.630574 2.739459 2.966666 2.843788 4.692793 3.172095
## 2 4.508034 3.931800 4.158120 3.883013 4.489959 4.495810 6.000236 5.001289
## 3 6.868421 8.203118 8.491487 7.681807 6.393702 6.492260 5.811685 5.987192
## 4 2.400338 4.464342 4.810546 3.603211 2.414290 2.493991 3.966604 1.972705
## 5 4.715695 4.926015 4.640776 4.444450 5.282563 5.152315 6.572203 5.683531
## 6 5.051112 6.412041 6.764467 5.837829 4.205067 4.305317 5.036975 3.815398
##        105      106      107      108      109      110      111      112
## 1 3.223448 3.618628 3.631351 3.259679 3.351457 3.335422 2.743048 2.868558
## 2 4.956142 5.253274 5.307506 4.990050 4.894350 5.027813 4.918935 4.183520
## 3 6.014221 5.881611 5.856170 5.944714 6.084653 5.930743 6.185964 6.069764
## 4 2.022137 2.164155 2.196416 2.023648 2.172750 2.042266 2.184470 2.238546
## 5 5.724543 6.103378 6.104999 5.770964 5.777602 5.809233 5.260548 5.174233
## 6 3.840428 3.789013 3.785737 3.801714 3.887880 3.778476 4.058712 3.895295
##        113      114      115      116      117      118      119      120
## 1 3.375466 3.392521 3.026294 3.301736 2.747953 2.915253 3.063432 2.869590
## 2 4.819193 4.774286 4.530960 4.818453 4.451377 4.589012 4.741708 4.798190
## 3 5.667421 5.680606 5.851170 5.493919 5.878062 5.869090 5.544023 5.809181
## 4 2.075218 2.106269 2.052459 2.053031 1.971584 1.988655 2.048517 2.045794
## 5 5.795991 5.778504 5.457332 5.719642 5.202323 5.373780 5.497713 5.357061
## 6 3.601847 3.596459 3.722199 3.590667 3.748787 3.744213 3.671311 3.776235
##        121      122      123      124      125      126      127      128
## 1 4.116775 4.252645 4.510005 5.058229 4.583383 4.343081 5.287399 4.735059
## 2 5.427653 5.424854 5.565025 6.069215 5.659762 5.427403 6.344535 5.999576
## 3 6.237519 6.139760 6.064278 6.001360 6.051491 6.152869 5.974765 5.445330
## 4 2.985164 2.938368 3.039074 3.475730 3.062302 2.973639 3.677551 2.962007
## 5 6.426088 6.565457 6.806695 7.305465 6.897705 6.650747 7.533959 7.099802
## 6 4.261019 4.162980 4.128457 4.247333 4.123875 4.163650 4.355189 3.161437
##        129      130      131      132      133      134      135      136
## 1 4.590233 4.791289 4.456575 4.402201 4.350142 4.479053 4.894039 4.582035
## 2 5.627423 6.120486 5.509731 5.516882 5.382470 5.779404 6.139554 5.674601
## 3 5.604137 5.425648 5.688097 5.652880 5.721190 5.497188 5.035538 5.535608
## 4 2.931268 3.021401 2.885217 2.835481 2.871725 2.806058 3.188140 2.903821
## 5 6.875876 7.154803 6.726119 6.686918 6.596530 6.821369 7.166802 6.870993
## 6 3.183589 3.174651 3.265601 3.234532 3.348169 3.162883 3.338913 3.134950
##        137      138      139      140      141      142      143      144
## 1 4.585322 4.572210 4.994596 4.721431 5.047669 6.196553 7.427033 6.200054
## 2 5.819167 5.632765 6.197877 5.809014 6.167568 7.327389 6.736697 6.601594
## 3 5.406565 5.610438 5.411571 5.483828 5.434774 5.732297 6.519423 5.705948
## 4 2.859060 2.928458 3.172007 2.969872 3.289011 4.421385 6.210476 4.719098
## 5 6.929533 6.859261 7.327005 7.036231 7.324311 8.387851 9.118028 8.148386
## 6 3.098547 3.195797 3.203702 3.121999 3.270140 4.203114 6.057048 4.491078
##        145      146      147      148      149      150      151      152
## 1 6.628290 6.634143 6.205886 6.539074 6.293195 5.851136 5.439102 5.651936
## 2 7.209721 6.881696 6.562636 7.091389 6.756783 6.281936 5.599987 6.029305
## 3 5.882920 5.395652 5.350043 5.860027 5.668932 5.539694 5.844264 5.733672
## 4 5.120965 5.221320 4.864032 5.058645 4.839584 4.425501 4.201583 4.325365
## 5 8.591160 8.420611 8.051529 8.485530 8.220414 7.812856 7.329573 7.603008
## 6 4.881773 4.956580 4.786268 4.850486 4.658853 4.298230 4.279168 4.390872
##        153      154      155      156      157      158      159      160
## 1 6.952748 5.752673 6.793409 7.450863 6.650053 4.652464 4.273222 5.383270
## 2 6.369979 6.052779 5.844207 6.703763 7.158351 4.234387 4.756091 5.368031
## 3 6.541819 5.446370 6.344801 6.334474 5.503366 6.942094 6.074535 6.178603
## 4 5.967163 4.417624 5.805631 6.566625 5.400744 4.276378 3.305864 4.351927
## 5 8.622664 7.648725 8.381752 8.889137 8.425203 6.168678 6.265700 7.165808
## 6 6.067192 4.403229 5.843835 6.536516 5.328492 5.218020 4.320602 4.558378
##        161      162      163      164      165      166      167      168
## 1 5.752473 5.237762 6.745173 6.961257 4.080738 4.385762 5.475612 4.315636
## 2 4.605274 4.398804 4.791045 4.937649 4.851661 4.926498 4.456582 5.095670
## 3 6.821006 7.745103 8.432676 8.695086 5.936568 5.663492 7.854149 5.576630
## 4 5.239834 5.208814 6.712875 6.998166 3.062315 3.452702 5.427142 3.432231
## 5 7.231434 6.383671 7.606315 7.711715 6.150603 6.283786 6.542030 6.239898
## 6 5.940257 6.088162 7.394089 7.646620 4.086328 4.360674 6.228342 4.356613
##        169      170      171      172      173      174      175      176
## 1 4.254128 4.150282 4.374444 4.230784 3.104965 2.654763 2.534686 2.342553
## 2 4.728033 4.677209 5.202442 5.015219 4.676351 4.091021 4.407110 4.153032
## 3 5.730715 5.794348 5.516106 5.715890 6.426784 6.668081 6.607389 7.275149
## 4 3.234454 3.142836 3.181914 3.077576 2.488721 2.512347 2.447970 3.326034
## 5 6.213385 6.133807 6.433082 6.312035 5.454412 4.962647 4.941506 4.309193
## 6 4.170734 4.100748 4.054129 4.025673 4.699883 4.808037 4.816591 5.496225
##        177      178      179      180      181      182      183      184
## 1 2.175426 2.365924 2.589714 2.810682 3.656914 2.701373 3.342620 2.979474
## 2 4.411335 4.095254 3.737891 3.879091 3.800673 4.190561 3.860567 4.061937
## 3 6.792977 6.830833 7.010189 7.720425 7.949502 7.318332 7.703218 7.337983
## 4 2.658053 2.616342 2.975519 3.642452 4.178165 3.143454 3.744498 3.144853
## 5 4.520962 4.655885 4.649899 4.604653 5.132343 4.814462 5.087352 5.054530
## 6 5.015596 4.976181 5.157347 5.945635 6.198433 5.587189 5.931557 5.543818
##        185      186      187      188      189      190      191      192
## 1 3.009695 2.461779 4.108449 2.947084 2.866184 3.171421 3.299804 2.939362
## 2 4.706625 4.276810 4.103340 4.369622 4.665779 4.360736 4.840840 4.814221
## 3 6.823279 6.988966 8.637738 7.764729 7.876360 8.097468 8.409214 8.135143
## 4 2.617485 2.667473 5.069928 4.220068 4.355866 4.608931 5.027524 4.620020
## 5 5.360158 4.777155 5.085672 3.049814 2.837691 2.892236 2.592917 2.487691
## 6 5.146074 5.258702 6.977902 6.090983 6.253786 6.421824 6.840347 6.509686
##        193      194      195       196      197      198      199      200
## 1 3.385297 3.423873 3.258558  5.286849 4.731058 4.582926 4.715512 4.639590
## 2 4.786382 5.315615 5.265629  5.625695 5.786297 5.784978 5.720384 6.255528
## 3 8.533158 8.873489 8.672208 10.009282 9.580192 9.208468 9.481059 9.444290
## 4 5.115810 5.261122 5.038340  6.858470 6.362933 6.100498 6.288002 6.311681
## 5 2.626002 2.323019 2.285881  3.495329 2.818135 2.825122 2.840680 2.451539
## 6 6.916387 7.350478 7.173909  8.592641 8.185358 7.919777 8.103044 7.965488
##        201      202      203       204       205      206      207
## 1 4.653597 3.907488 4.866644  5.491799  5.693985 2.252863 1.996460
## 2 6.272606 5.771920 5.739854  5.855113  5.957838 4.945805 4.407545
## 3 9.430213 8.587107 9.723775 10.042167 10.216692 6.837520 6.613955
## 4 6.329487 5.273529 6.554190  6.965429  7.174721 2.820461 2.266758
## 5 2.456624 2.615509 2.868195  3.538131  3.737246 4.677119 4.523642
## 6 7.969849 7.156198 8.271244  8.640337  8.822374 4.966944 4.612421
##        208      209      210      211      212      213      214      215
## 1 2.684155 4.559083 5.472929 4.942984 5.391736 4.669469 2.088834 4.075614
## 2 4.839209 4.297639 5.214301 4.486356 5.179802 4.522080 4.441468 6.128544
## 3 6.238923 7.056415 6.886396 6.882747 6.859678 7.017952 6.877368 6.804923
## 4 2.088170 4.501917 4.748828 4.456045 4.719108 4.521710 2.774437 3.942573
## 5 5.173003 6.173648 7.246427 6.672900 7.168488 6.358530 4.424781 5.994199
## 6 4.338029 5.951508 5.923358 5.785418 5.899356 5.931043 4.957469 5.447417
##        216      217      218      219      220      221      222      223
## 1 2.008161 4.833935 2.937343 5.253528 4.990749 4.858718 5.140070 4.702050
## 2 4.493721 4.333352 3.927803 4.474244 4.098632 3.855927 4.507648 3.793452
## 3 6.701716 6.923664 6.481639 6.712071 6.932306 7.216588 6.814993 7.226735
## 4 2.431159 4.642940 2.604000 4.619602 4.623502 4.781149 4.654611 4.752233
## 5 4.533967 6.461391 5.090497 6.968673 6.593520 6.320911 6.799719 6.130295
## 6 4.734048 5.887167 4.502200 5.707295 5.831456 6.068344 5.809474 6.083430
##        224      225      226      227      228      229      230      231
## 1 2.510992 4.285144 5.053107 3.807224 2.766869 3.866941 2.455648 2.274652
## 2 3.859178 3.856074 4.287951 3.761622 3.704484 4.327344 4.344291 4.345763
## 3 6.676587 8.061548 8.562013 7.691942 6.882629 8.267586 7.279603 6.313200
## 4 2.632171 4.857703 5.604564 4.291528 3.086868 5.025870 3.556647 2.180368
## 5 4.738585 5.386965 5.891695 5.165618 4.738937 4.794451 4.323631 4.757661
## 6 4.706017 6.297753 6.888318 5.848384 4.984604 6.583434 5.480585 4.442431
##        232      233      234      235      236      237      238      239
## 1 2.845373 4.094662 4.348568 4.582063 2.164414 4.571565 2.701412 2.261099
## 2 3.705792 3.899503 3.957570 3.819634 4.306913 3.896708 3.743105 4.916380
## 3 7.086045 8.097837 8.247294 7.256051 6.371138 7.184499 7.164135 7.763565
## 4 3.349510 4.826196 5.081229 4.795798 2.237307 4.663958 3.356024 4.006857
## 5 4.653852 5.135020 5.281054 5.952219 4.649091 6.018095 4.490512 2.932965
## 6 5.189298 6.314557 6.527581 6.182940 4.494536 6.049817 5.234441 6.100275
##        240      241      242      243      244      245      246      247
## 1 2.096391 2.294939 2.317889 2.233775 2.546385 2.843542 3.010876 2.298688
## 2 4.609977 4.554218 4.836874 4.812866 5.251961 5.595467 5.698876 5.306870
## 3 7.513168 7.401734 7.139957 7.340494 8.034419 6.985702 6.964082 7.498675
## 4 3.612081 3.523994 3.199172 3.505714 4.402069 3.307803 3.374542 3.724642
## 5 2.956836 3.179208 3.559831 3.174838 2.972611 4.361124 4.499702 3.449697
## 6 5.782692 5.644636 5.418724 5.676824 6.431679 5.293119 5.282827 5.730382
##        248      249      250      251      252      253      254      255
## 1 2.612063 2.227342 2.472500 2.394111 2.529107 3.107181 4.463447 4.036629
## 2 5.197006 5.027748 5.230422 5.313640 5.406880 5.560045 5.671848 6.347835
## 3 7.123772 7.327359 7.884487 7.824608 7.913195 8.440374 9.349847 8.962012
## 4 3.315715 3.504649 4.188133 4.105072 4.276291 4.932310 6.124355 5.596288
## 5 3.980918 3.426770 3.241080 3.321813 3.366958 3.347543 4.117622 2.565061
## 6 5.332551 5.535635 6.098106 6.038428 6.187201 6.732703 7.679236 7.519106
##        256      257      258      259      260      261      262      263
## 1 4.155292 4.824381 5.880265 4.531389 4.220084 4.147002 4.710711 5.556397
## 2 6.580192 5.707147 4.551395 3.894518 4.041152 3.826233 3.886648 4.317743
## 3 8.992176 9.653615 9.041679 7.629278 7.342798 7.487956 8.005527 8.762238
## 4 5.699971 6.403033 6.371304 4.637079 4.211517 4.374564 5.047654 6.017951
## 5 2.791995 2.848603 6.222798 5.518648 5.417650 5.174118 5.463188 5.971533
## 6 7.592589 8.192506 7.613422 6.131531 5.811603 5.956088 6.518475 7.320962
##        264      265      266      267      268      269      270      271
## 1 4.321892 4.369594 3.895689 4.141040 5.180260 4.218897 4.554789 2.091791
## 2 3.944671 3.834643 4.846831 4.038079 4.202225 3.938496 4.496815 4.877938
## 3 7.399951 7.595295 6.985984 7.167083 8.815364 8.370001 7.374525 6.914144
## 4 4.361095 4.543509 3.870860 4.139572 5.912661 5.158425 4.630825 2.685788
## 5 5.408871 5.364474 5.369503 5.311210 5.468341 4.684862 5.853082 4.111760
## 6 5.873985 6.084410 5.582297 5.691716 7.365150 6.850545 6.292985 5.143047
##        272      273      274      275      276      277      278      279
## 1 2.111547 1.993339 4.916068 4.705346 2.513014 4.779926 4.711343 2.285977
## 2 4.839362 4.286203 4.148237 4.380952 4.296659 4.210140 4.469765 4.503523
## 3 7.520064 7.064234 8.378912 8.386111 7.804129 8.364865 8.493915 7.564951
## 4 3.441028 2.665134 5.602045 5.598971 3.877658 5.612713 5.710319 3.612360
## 5 3.725629 3.921894 5.523364 4.973350 3.058289 4.960332 4.861229 3.070627
## 6 5.736385 5.155769 7.186009 7.238343 6.006291 7.213299 7.355393 5.795132
##        280      281      282      283       284      285      286      287
## 1 2.790991 3.947935 2.795306 5.515391  6.980733 4.422708 3.123714 4.079753
## 2 4.173615 3.975881 4.184062 4.440886  6.268257 5.936035 5.428512 6.501695
## 3 8.122015 8.703650 8.222715 9.258337 10.933893 9.363760 8.450626 8.767961
## 4 4.285849 5.167816 4.389736 6.563865  8.344298 6.072569 4.772330 5.471836
## 5 3.561388 4.216680 3.374750 5.571630  5.408034 2.345212 2.477254 2.927409
## 6 6.491419 7.105591 6.595419 8.219563  9.973467 7.928601 6.930874 7.445534
##        288      289      290      291      292      293      294      295
## 1 2.756566 2.716768 2.843575 3.668080 3.989047 3.608840 2.497209 2.387338
## 2 5.343188 5.207865 5.287981 5.569837 5.473392 5.650164 4.941731 4.796956
## 3 8.041222 7.905277 8.061694 8.582296 8.865102 8.509272 7.358111 6.995131
## 4 4.396113 4.214046 4.535171 5.074359 5.432490 5.006978 3.576391 3.058148
## 5 2.509960 2.585977 2.400932 2.683501 2.813661 2.686863 4.345410 4.479945
## 6 6.413629 6.249971 6.494620 6.930359 7.229010 6.875832 5.592884 5.181463
##        296      297      298      299      300      301      302      303
## 1 2.491650 2.393691 2.750925 3.788908 4.039227 3.734424 2.232083 2.376030
## 2 4.531097 4.405035 5.064135 5.938158 5.803548 5.507286 4.604003 4.762883
## 3 7.527753 7.235927 6.829083 8.722048 9.087048 8.634313 7.337638 7.605560
## 4 3.690081 3.281272 3.028002 5.478237 5.872746 5.284133 3.549854 3.969692
## 5 4.101880 4.215355 4.808009 2.456182 2.336450 2.394242 3.068423 2.941845
## 6 5.702683 5.379947 5.078750 7.315906 7.652372 7.121431 5.575441 5.929574
##        304      305      306      307      308      309      310      311
## 1 2.734380 2.774880 2.361712 3.039054 2.564437 2.631756 2.582365 3.293284
## 2 4.561650 4.196815 4.266337 4.017975 4.168573 4.202224 4.535816 5.594347
## 3 8.039455 7.868780 7.241481 7.658684 7.290961 6.546441 6.168368 6.187983
## 4 4.456508 4.073418 3.192729 3.845712 3.261918 2.344549 1.843049 2.847242
## 5 2.855273 3.436586 3.684046 3.978741 3.854584 4.998677 5.185967 5.719675
## 6 6.336183 6.106407 5.435975 5.842022 5.453779 4.466812 4.127252 4.624290
##        312      313      314      315      316      317      318      319
## 1 2.402724 2.904430 2.574041 2.606905 2.711944 2.970980 2.658070 2.293294
## 2 4.476328 4.594927 4.348331 4.178642 4.891978 4.889172 4.794276 4.262780
## 3 6.476538 6.069256 6.284457 6.344720 6.136907 5.966724 6.078177 6.307673
## 4 2.297145 1.850451 1.966400 2.079641 1.938234 1.959365 1.906122 1.969043
## 5 4.937184 5.460141 5.098426 5.028976 5.293601 5.460000 5.215271 4.838880
## 6 4.467670 4.023815 4.217072 4.259199 4.159874 4.029901 4.115912 4.245101
##        320      321      322      323      324      325      326      327
## 1 2.254385 1.961575 1.975895 2.005309 2.474246 1.958008 2.220200 2.005009
## 2 4.534455 4.506705 4.537503 4.811214 4.996492 4.590902 4.987239 4.873621
## 3 6.250142 6.816256 6.795014 6.731231 6.397846 6.994614 7.395759 7.139104
## 4 1.963279 2.473559 2.434921 2.406058 2.072533 2.775066 3.465771 3.042737
## 5 4.842685 4.449705 4.484974 4.585730 5.057642 4.334264 4.248227 4.277525
## 6 4.240360 4.799353 4.776401 4.757420 4.457601 5.018766 5.526219 5.218292
##        328      329      330      331      332      333      334      335
## 1 2.035908 2.350131 2.312741 2.230762 2.580536 2.403384 2.205917 2.203234
## 2 4.877258 5.105806 4.900165 4.957900 5.488342 5.279997 5.053555 5.093833
## 3 6.725423 7.068932 7.323299 7.087469 7.538669 7.661878 7.331467 7.267954
## 4 2.517471 3.305539 3.594198 3.303857 3.752625 3.884973 3.226203 3.160665
## 5 4.511237 4.336792 4.068778 4.134704 3.513382 3.127529 4.341878 4.382338
## 6 4.796609 5.372181 5.613018 5.389720 5.942590 6.090114 5.485716 5.424178
##        336      337      338      339      340      341      342      343
## 1 2.186727 2.223765 2.322145 2.157595 2.205404 2.271625 3.007511 2.617582
## 2 5.156767 5.112746 5.110110 5.016265 5.044939 4.987777 4.514441 4.784059
## 3 7.189428 6.898354 6.809633 7.000683 6.865025 6.777057 8.446366 7.165257
## 4 3.056405 2.647838 2.524309 2.778949 2.590159 2.417256 4.633530 3.331511
## 5 4.485929 4.729718 4.812037 4.629130 4.744764 4.837628 2.808348 4.362098
## 6 5.347455 5.032342 4.935566 5.124323 4.979263 4.865181 6.823411 5.448631
##        344      345      346      347      348      349      350      351
## 1 2.672462 2.940953 2.423474 2.543259 3.956148 3.799867 3.066109 2.914978
## 2 4.757585 4.927327 5.421207 5.492314 6.193135 5.934386 5.524385 5.618626
## 3 7.461637 8.044999 7.305607 7.150579 8.715944 8.820537 8.437225 8.219455
## 4 3.802233 4.548197 3.369890 3.310384 5.476084 5.437402 4.699873 4.404974
## 5 2.778316 2.366030 4.320697 4.466973 2.400876 2.213242 2.925825 3.077473
## 6 5.684615 6.353501 5.565696 5.487424 7.183736 7.319965 6.755452 6.526367
##        352      353       354      355      356      357      358      359
## 1 3.882617 4.035485  5.152518 4.846552 4.789845 6.995105 6.715094 6.630405
## 2 6.269580 6.763444  7.013329 7.521812 7.391746 6.500890 6.156007 6.172788
## 3 8.766898 8.789968 10.015464 9.117773 9.216143 5.456756 5.735313 5.758167
## 4 5.498901 5.585389  6.831393 6.079486 6.140023 5.907878 5.797393 5.786448
## 5 2.847020 3.254646  3.220675 4.018615 3.810904 8.663013 8.315674 8.225028
## 6 7.254755 7.319860  8.653858 7.691074 7.769223 4.621450 4.715344 4.749805
##        360      361      362      363      364      365      366      367
## 1 5.340790 5.361453 5.509807 5.682415 6.883975 7.392644 6.618984 5.752759
## 2 6.182375 6.025704 6.270247 6.585418 6.526351 6.398780 7.725367 6.760694
## 3 4.964540 5.192113 4.710339 4.977082 5.468166 7.258598 5.920246 4.657637
## 4 4.207340 4.344110 4.232657 4.378729 5.817697 6.961379 5.534146 4.394301
## 5 7.278021 7.215827 7.464845 7.699553 8.598542 8.542707 8.530898 7.799054
## 6 2.467536 2.805217 2.333627 2.769367 4.652258 6.207836 4.624219 2.863464
##        368      369      370      371      372      373      374      375
## 1 7.067897 6.260581 7.113702 7.203297 5.999323 7.191112 6.985946 7.809059
## 2 8.102215 6.662121 5.980344 5.981912 6.056314 6.266885 7.931335 8.798177
## 3 5.030692 6.544359 7.110024 7.285275 6.133890 6.680971 4.907474 5.412285
## 4 5.856186 5.544439 6.673537 6.801323 5.253873 6.587324 5.414656 6.304659
## 5 8.916963 7.876027 8.330037 8.374001 7.563523 8.532946 8.984585 9.736883
## 6 4.770107 4.939445 6.154059 6.280595 4.367286 5.917457 3.722610 4.767839
##        376      377      378      379      380       381      382      383
## 1 6.017927 6.001561 5.834679 6.394230 6.161471 11.755809 6.015300 6.112881
## 2 6.521129 6.722821 6.526672 7.136095 7.001182 12.116793 6.800516 7.087802
## 3 5.207964 4.521828 4.798660 4.679079 4.555128  9.663159 4.661952 4.529605
## 4 4.944969 4.652842 4.462833 5.083590 4.746519 11.163729 4.647065 4.555749
## 5 7.795568 7.924073 7.787937 8.275536 8.137870 12.821812 7.966471 8.185980
## 6 3.171317 2.549903 2.441746 3.030744 2.612679  9.894275 2.535543 2.500835
##        384      385      386      387      388      389      390      391
## 1 6.125147 7.394570 6.913418 7.236737 7.210917 6.892982 5.975128 5.602574
## 2 7.080988 8.494759 7.900222 8.245243 8.254546 7.913604 7.016902 6.584068
## 3 4.555957 4.593328 4.644399 4.813092 4.798740 4.585380 4.568138 4.613924
## 4 4.556169 5.912501 5.372451 5.774218 5.768068 5.325687 4.443905 4.162427
## 5 8.197628 9.349727 8.910330 9.193301 9.152365 8.907678 8.068366 7.677835
## 6 2.535837 4.207220 3.430304 3.965891 3.922688 3.476912 2.460378 2.173913
##        392      393      394      395      396      397      398      399
## 1 5.308181 6.426170 5.489378 5.726631 5.638596 5.653609 5.914268 7.989079
## 2 6.187448 7.518315 6.415998 6.699030 6.447121 6.503431 6.938797 8.851159
## 3 4.690817 4.584419 4.693783 4.560825 4.700448 4.730041 4.591516 5.349991
## 4 4.059557 4.886988 4.152838 4.352711 4.260815 4.238695 4.395922 6.683507
## 5 7.292592 8.487710 7.524344 7.763687 7.641959 7.677186 8.002371 9.767720
## 6 2.226899 2.983346 2.121055 2.265178 2.176869 2.197872 2.365545 4.881189
##        400      401      402      403      404      405       406      407
## 1 6.400821 6.900501 6.095533 5.820693 6.578583 7.840199 10.009989 7.043493
## 2 7.463474 7.776663 6.969235 6.627020 7.610294 8.716935 10.666205 8.166864
## 3 4.478721 4.713832 4.642624 4.544640 4.647198 5.160244  7.453981 4.877537
## 4 4.959465 5.497801 4.669753 4.375806 5.225734 6.673070  9.081383 5.594715
## 5 8.388325 8.804129 8.087911 7.825523 8.542363 9.552186 11.432606 9.054357
## 6 3.040490 3.471798 2.536220 2.216427 3.279024 4.910238  7.595875 3.994384
##        408      409      410       411      412      413      414
## 1 5.605718 5.909119 6.091597  9.003486 6.898431 8.111524 6.840670
## 2 6.317911 6.868005 6.505072  9.624185 7.490223 8.978058 7.777474
## 3 4.684219 4.359127 4.498617  6.248943 4.283169 4.939472 4.338544
## 4 4.354104 4.411296 4.984789  8.179599 5.753451 6.786214 5.592512
## 5 7.570396 7.942399 7.792350 10.375746 8.547474 9.825302 8.675535
## 6 2.751697 2.684071 3.512054  6.953620 4.401901 5.499975 4.000683
##         415      416      417      418       419      420      421
## 1  9.673650 7.712449 7.339434 7.393740 11.103741 7.052418 5.696703
## 2 10.507271 8.404166 8.034930 8.321960 11.623093 7.789436 6.412841
## 3  6.006137 4.335355 4.349290 4.150387  7.922425 4.248090 4.384831
## 4  8.419684 6.420781 6.131883 6.083046 10.281934 5.925977 4.353909
## 5 11.272810 9.378378 8.990931 9.186224 12.321200 8.708818 7.649405
## 6  6.897999 4.863694 4.682977 4.396530  8.940339 4.410853 2.275161
##        422      423      424      425      426      427      428      429
## 1 5.633333 5.359069 6.942560 6.733842 7.490506 6.578878 7.976041 6.437220
## 2 6.540748 6.403579 7.807594 7.858624 8.314551 7.743199 8.719330 7.368712
## 3 4.337641 4.332067 4.287058 4.467816 4.150667 4.462962 5.101391 3.944706
## 4 4.220529 4.151550 5.802264 5.736500 6.219861 5.681331 7.081609 5.224531
## 5 7.665560 7.356701 8.626415 8.441732 9.201143 8.265300 9.461813 8.241034
## 6 2.180745 2.508136 4.580701 4.667841 4.741929 4.569780 5.669296 3.716263
##        430      431      432      433      434      435      436      437
## 1 6.946892 6.173374 6.398175 5.806166 6.272329 6.545788 6.747217 7.180959
## 2 7.717744 7.082043 7.134695 6.790664 7.058495 7.351287 7.386206 7.874780
## 3 4.007002 4.145470 4.267862 4.462911 4.105578 3.959357 3.969580 4.147301
## 4 5.654346 5.081922 5.298499 4.904392 5.104864 5.332295 5.460661 5.994414
## 5 8.696259 7.931573 8.088359 7.540841 8.039230 8.323316 8.488298 8.855936
## 6 4.134503 3.794201 3.973307 3.782250 3.619313 3.721287 3.748221 4.457023
##        438      439      440      441      442      443      444      445
## 1 7.707796 7.700430 6.054304 6.446442 5.755888 5.609025 5.811139 6.418899
## 2 8.394236 8.484083 7.006532 7.350912 6.477458 6.351316 6.518089 7.288150
## 3 4.213632 4.342741 4.516857 4.509393 4.608229 4.755074 4.637509 3.918161
## 4 6.386947 6.327543 4.552303 5.079909 4.380978 4.219854 4.422982 4.931360
## 5 9.396533 9.435637 8.099988 8.384080 7.713189 7.607664 7.769558 8.369895
## 6 4.811409 4.695929 2.433801 2.937692 2.207332 2.192461 2.239337 2.871156
##        446      447      448      449      450      451      452      453
## 1 7.168870 5.714462 5.725384 5.655226 5.767098 7.013088 5.561251 5.419305
## 2 7.822260 6.503523 6.592742 6.535974 6.588139 7.643669 6.322861 6.315283
## 3 4.052052 4.377493 4.626360 4.619462 4.290924 4.436440 4.659548 4.698128
## 4 5.892565 4.313539 4.358067 4.262430 4.358030 5.922856 4.230132 4.095576
## 5 8.869383 7.692356 7.719083 7.662880 7.734779 8.619425 7.510000 7.420544
## 6 4.315338 2.223077 2.207460 2.126064 2.281549 4.610171 2.238968 2.096565
##        454      455      456      457      458      459      460      461
## 1 5.704355 7.016989 6.589745 6.913673 6.866815 5.484535 5.216250 5.566897
## 2 6.201144 7.628370 7.338629 7.770650 7.771267 6.463171 6.189770 6.350729
## 3 5.115979 4.321729 4.205509 4.252759 4.258883 4.236988 4.789795 4.385829
## 4 4.575975 5.918794 5.464389 5.749648 5.801327 4.266589 4.034746 4.354303
## 5 7.488797 8.615112 8.280416 8.613939 8.529698 7.413953 7.197961 7.441333
## 6 2.816811 4.549028 4.088681 4.450875 4.516870 2.377775 2.175860 2.581875
##        462      463      464      465      466      467      468      469
## 1 5.245377 5.175440 5.168781 4.825356 4.830816 6.492417 5.264318 5.133202
## 2 6.150612 6.113243 6.000826 5.995825 6.265945 7.375314 6.308918 6.388907
## 3 4.871657 4.859767 5.057480 4.982831 4.910892 4.372635 4.541440 4.648344
## 4 4.028929 4.046035 4.100801 3.940852 4.014623 5.454107 3.942868 4.149291
## 5 7.224008 7.129990 7.081032 6.761263 6.784776 8.163381 7.276556 7.067634
## 6 2.197525 2.207914 2.436963 2.400811 2.744376 4.323182 2.345760 2.548814
##        470      471      472      473      474      475      476      477
## 1 4.901491 4.816208 4.722700 4.659061 4.806139 5.415982 5.640980 5.208138
## 2 6.323801 5.996607 5.921288 5.766964 5.546301 6.666256 6.654666 6.151680
## 3 4.973919 4.971853 5.181002 5.161492 5.472082 4.527575 4.374497 4.862825
## 4 4.089454 3.742729 3.729722 3.771861 4.165060 4.053967 4.237950 3.945356
## 5 6.854434 6.816620 6.708850 6.594683 6.547902 7.501061 7.637258 7.206421
## 6 2.734118 2.311425 2.560930 2.499788 3.048901 2.434451 2.523020 2.223730
##        478      479      480      481      482      483      484      485
## 1 6.105211 5.382604 5.214375 4.428172 4.526780 4.616956 4.359692 4.497564
## 2 7.232034 6.384794 6.121775 5.803910 5.637316 5.575392 6.127800 6.133862
## 3 4.341229 4.588992 4.776866 5.387882 5.594453 5.786301 5.679794 5.329406
## 4 4.658359 4.056417 4.119396 3.815854 3.967622 4.139027 4.014627 3.945221
## 5 8.130462 7.407656 7.165587 6.328683 6.331364 6.329978 6.225603 6.407568
## 6 2.819877 2.149619 2.407795 2.841677 3.062382 3.305435 3.354835 3.007909
##        486      487      488      489      490      491      492      493
## 1 4.392934 4.727043 4.539202 5.655817 6.220009 6.633606 5.634401 5.208921
## 2 5.880484 6.022019 6.037518 6.749983 7.341242 7.743312 6.605526 6.225869
## 3 5.440584 4.972136 5.232320 5.468533 5.317298 5.364747 5.458144 5.624994
## 4 3.898792 3.748954 3.856913 4.067660 4.515760 4.922601 4.029950 3.853652
## 5 6.249662 6.708037 6.500512 7.746032 8.298286 8.670417 7.692851 7.224881
## 6 2.935648 2.355052 2.761430 3.339171 3.627365 4.033745 3.245201 3.298803
##        494      495      496      497      498      499      500      501
## 1 2.874711 2.815571 3.076628 3.570270 2.982151 2.867755 3.260115 3.150398
## 2 4.881230 4.787928 5.205773 5.359640 4.929451 4.699697 5.121802 4.887289
## 3 5.924585 6.035621 6.099197 5.645035 5.731891 5.820200 5.672489 5.683894
## 4 2.137355 2.335780 2.705197 2.322917 1.885295 1.925584 2.012924 1.880862
## 5 5.413752 5.273091 5.426681 6.011801 5.542505 5.414893 5.810165 5.688309
## 6 3.852454 3.986370 4.203245 3.717331 3.612856 3.666547 3.612745 3.521661
##        502      503      504      505      506
## 1 3.056424 3.190441 3.518096 3.391335 3.469554
## 2 4.737543 4.960431 4.668497 4.734780 5.374542
## 3 6.417801 6.310447 6.682914 6.547413 6.324684
## 4 2.309307 2.174338 2.764118 2.545516 2.378712
## 5 5.506603 5.730311 5.775209 5.732539 5.991382
## 6 4.294822 4.194855 4.555464 4.415418 4.255945
## 
## Clustering vector:
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 
##   1   1   1   1   1   1   1   4   4   1   4   1   1   4   4   4   1   4 
##  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36 
##   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
##  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54 
##   4   1   1   5   5   1   1   1   1   1   1   4   4   1   1   1   1   1 
##  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72 
##   5   5   5   5   1   1   1   1   1   1   5   5   5   1   1   1   1   1 
##  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90 
##   1   1   1   1   4   4   4   1   1   1   1   1   1   1   1   1   1   1 
##  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 
##   1   1   1   1   4   1   1   2   2   1   4   4   4   4   4   4   4   4 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 
##   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   6   4   4 
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 
##   6   6   6   6   4   4   4   4   6   6   6   6   6   6   6   6   3   6 
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 
##   3   3   3   3   3   6   6   6   3   6   3   3   3   2   4   6   2   2 
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
##   2   2   4   4   2   4   4   4   4   4   4   4   4   1   1   1   1   1 
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 
##   2   1   1   1   4   1   2   1   1   5   5   5   5   5   5   5   5   5 
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 
##   5   5   5   5   5   5   5   1   1   4   2   2   2   2   2   1   4   1 
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 
##   2   4   2   2   2   2   2   1   2   2   2   1   2   1   4   1   2   2 
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 
##   2   4   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 
##   5   5   5   5   5   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 
##   1   1   1   2   2   1   2   2   1   1   2   1   2   5   5   5   5   1 
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 
##   1   5   5   5   5   1   1   1   1   1   5   5   5   1   1   1   1   1 
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 
##   1   1   4   4   4   4   4   4   4   4   4   4   4   4   1   1   1   4 
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   5 
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 
##   1   1   5   1   1   5   5   5   1   5   5   5   5   5   3   3   3   6 
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 
##   6   6   6   3   3   3   6   3   3   3   3   6   3   3   3   6   6   6 
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 
##   6   6   3   6   6   6   3   3   3   3   3   6   6   6   6   6   6   6 
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 
##   6   6   3   6   3   6   6   6   3   3   3   6   6   6   3   3   3   3 
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 
##   3   3   3   3   3   3   6   6   6   3   3   3   3   3   3   3   6   3 
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 
##   6   6   3   3   3   3   3   6   6   6   6   6   6   3   6   6   6   6 
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 
##   3   6   6   6   3   3   3   3   6   6   6   6   6   6   6   6   3   6 
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 
##   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6 
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 
##   6   6   6   6   3   6   6   4   4   4   4   4   4   4   4   4   4   4 
## 505 506 
##   4   4 
## 
## Within cluster sum of squares by cluster:
## [1] 25160.33 21548.41 55771.90 22667.20 17974.37 26811.41
##  (between_SS / total_SS =  77.3 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"         "iter"        
## [9] "ifault"

We can see that each of the 506 observations are assigned to the 1-6 clusters. Within cluster sum of squares by cluster is 76.8 %. The total_SS is calculated as the sum of squared distance of each observation to the global mean of the observations. between_SS is the sum of squared distance within each cluster. K-means mimimizes the within cluster sum of squares (the dispersion in each group) to inter cluster sum of squares. By assigning the observations to 6 clusters rather than assigning each sample to form its own cluster, 76.8 % of reduction is achieved in sum of squares.

Visualization of clusters on the boston_scaled data

To have fewer colours for visualization we again do the k-means algorithm with k = 4. Here we can see the pairwise plot of the clusters and the variables. The pair plot shows what is the representation of different clusters when we plot the observations pair-wise. For example, when we plot “crim” with “rad” only red and blue clusters are presnet and they are well separated, wheras the plot of age with dis the clusters are not well separated.

```r # Kmeans algorithm km <-kmeans(dist_eu, centers = 4)

pairs(boston_scaled, col = km$cluster) ```

8.LDA on Boston data with clusters as target classes

Here we do LDA on Boston data with the target classes as the clusters. The clusters are obtained using the k-means algorithm. For this we first scale the Boston data set. We take the number of centers to be 4 forthe k-means algorithm. The predictand for the LDA is the km$cluster and we append that column to the boston_scaled data set. We use the whole boston_scaled data for training the model. lda.fit.cluster is our model obtained using LDA algorithm.

library(MASS)

# load the data
data("Boston")

# center and standardize variables
boston_scaled <- scale(Boston)
km <-kmeans(boston_scaled, centers = 4)
cluster_boston <- data.frame(km$cluster, boston_scaled)
lda.fit.cluster <- lda(km.cluster~., data = cluster_boston)
# print the lda.fit object
lda.fit.cluster
## Call:
## lda(km.cluster ~ ., data = cluster_boston)
## 
## Prior probabilities of groups:
##          1          2          3          4 
## 0.43873518 0.29446640 0.19960474 0.06719368 
## 
## Group means:
##         crim         zn      indus       chas        nox         rm
## 1 -0.3773355 -0.3363976 -0.2956962 -0.2723291 -0.3273607 -0.1096213
## 2  0.8837711 -0.4872402  1.1114442 -0.2723291  1.0786332 -0.5114199
## 3 -0.4075518  1.5458153 -1.0839650 -0.2333479 -1.0006627  0.9026214
## 4 -0.1985497 -0.2602436  0.2799956  3.6647712  0.3830784  0.2756681
##           age         dis          rad        tax     ptratio      black
## 1 -0.05231647  0.06844179 -0.589736962 -0.5917014 -0.02981258  0.2845105
## 2  0.79225008 -0.84617490  1.282689903  1.3410968  0.64457655 -0.7027388
## 3 -1.17904458  1.23365823 -0.596395633 -0.6450354 -0.75326659  0.3536070
## 4  0.37213224 -0.40333817  0.001081444 -0.0975633 -0.39245849  0.1715427
##        lstat         medv
## 1 -0.1901451 -0.003273163
## 2  0.9577596 -0.773286992
## 3 -0.9396631  0.954978347
## 4 -0.1643525  0.573340910
## 
## Coefficients of linear discriminants:
##                 LD1          LD2         LD3
## crim    -0.03588743  0.012919433 -0.13892581
## zn      -0.26899879  0.161861225 -1.14459979
## indus    0.02111139 -0.522963529 -0.10581880
## chas     5.83217824  0.139883098 -0.07950872
## nox      0.02388256 -0.339767952 -0.54634096
## rm      -0.07667284  0.122149108 -0.40616332
## age      0.09549418  0.003087882  0.51489883
## dis      0.05281516  0.218241639 -0.47061603
## rad      0.07372900 -0.882986434 -0.15659795
## tax      0.02758388 -0.567822852 -0.71029059
## ptratio -0.01120987 -0.031444139 -0.03665274
## black    0.01450393  0.048014580  0.03593244
## lstat   -0.13681164 -0.273500489 -0.56189503
## medv    -0.15412087  0.158948817 -0.60570843
## 
## Proportion of trace:
##    LD1    LD2    LD3 
## 0.8118 0.1479 0.0403

The model lda.fit.cluster has three discriminants as we used 4 clusters. The first discriminant LD1 explains 70 % of the variance followed by LD2 and LD3 explain 19% and 10 % variance respectively. The variable black has negative coefficient of 1.87 for LD2. The variable crime has a coefficient 0.8 for LD2. The variable nox has relatively high coefficients for LD1 and LD3. The variable tax has also high coefficents for LD1 and LD3 (0.78 and 0.63).

Biplot of the lda.fit.cluster model

Here we obtain the bipot and we can see that “black” has the largest coefficent (negative) for LD2. The variable crime has a positive arrow for LD2

# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}
# target classes as numeric
classes <- as.numeric(cluster_boston$km.cluster)
# plot the lda results
plot(lda.fit.cluster, dimen = 2, col = classes, pch = classes)
lda.arrows(lda.fit.cluster, myscale = 2)

9.model predictors and lda.fit$scaling for the “crime variable” and cluster

Here we get a 3-d plot with LD1 in the x axis, Ld2 in y-axis and LD3 in the z-axis. lda.fit$scaling aa matrix which transforms observations to discriminant functions, normalized so that within groups covariance matrix is spherical. The plot shows the classification based on the crime variable. We can see that there is some overlap between low, medium low and medium high crime classes,

model_predictors <- dplyr::select(train, -crime)
# check the dimensions
dim(model_predictors)
## [1] 404  13
dim(lda.fit$scaling)
## [1] 13  3
# matrix multiplication
matrix_product <- as.matrix(model_predictors) %*% lda.fit$scaling
matrix_product <- as.data.frame(matrix_product)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color = train$crime)

#### model predictors and lda.fit.cluster$scaling for the “cluster variable”

We call the scaled Boston data set added with cluster as the first column as cluster_boston. We use the cluster_boston data set /which as 506 rows and 15 columns) for the linear discriminant analysis. We get lda.fit.cluster as the lda model we trained when the predictand is the km$cluster obtained from the kmeans algorithm.

The plot shows the four clusters. They are quite well separated.

model_predictors <- dplyr::select(cluster_boston, -km.cluster)
# check the dimensions
dim(model_predictors)
## [1] 506  14
dim(lda.fit.cluster$scaling)
## [1] 14  3
# matrix multiplication
matrix_product <- as.matrix(model_predictors) %*% lda.fit.cluster$scaling
matrix_product <- as.data.frame(matrix_product)
library(plotly)
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color =cluster_boston$km.cluster)